// test_P3EasyMesh.cpp
#include "Mesh.h"
#include <fstream>
using namespace std;

void test_easymesh(int order) {
    EasyMesh *mesh = new EasyMesh("D",order);
    int numgrid = mesh->getNumGrid();
    cout << "number of grids is:" << numgrid << endl;
    int numdofs = mesh->getTotalNumDofs();
    cout << "number of dofs is:" << numdofs << endl;
    Grid<2> g0 = mesh->getGrid(0);
    Grid<2> g1 = mesh->getGrid(1);
    vector<Point<2>> p0 = g0.getVertices();
    cout << "第0个网格的顶点坐标为:\n";
    for (auto &p : p0) {
        cout << p;
    }
    cout << endl;
    vector<Point<2>> p1 = g1.getVertices();
    cout << "第1个网格的顶点坐标为:\n";
    for (auto &p : p1) {
        cout << p;
    }
    cout << endl;
    vector<pair<int, int>> index_bndmark0 = mesh->getIndexofGrid(0);
    cout << "第0个网格的自由度编号、boundarymark为:\n";
    for (auto &ib : index_bndmark0) {
        cout << "index: " << ib.first << ",boundarymark: " << ib.second << "\n";
    }
    cout << endl;
    set<Dofs<2>> boundarydofs = mesh->getBoundaryDofs();
    cout << "边界上的自由度为:\n";
    for (auto it = boundarydofs.begin(); it != boundarydofs.end(); ++it) {
        cout << *it << "\n";
    }
    cout << endl;

}

void test_distributeDofs(int order) {
    EasyMesh *mesh = new EasyMesh("D",order);
    int numgrid = mesh->getNumGrid();
    cout << "number of grids is:" << numgrid << endl;
    int numdofs = mesh->getTotalNumDofs();
    cout << "number of dofs is:" << numdofs << endl;
    int numedges = mesh->getNumEdge();
    cout << "number of edges is:" << numedges << endl;
    auto bnddofs = mesh->getBoundaryDofs();
    for (auto dof : bnddofs) {
      int index = dof.getGlobalIndex();
      int bndmark = mesh->getBoundaryMark(index);
      Point<2> pnt = dof.getPoint();
      // cout << "index: " << index <<", bndmark:" << bndmark << "\n";
      cout << pnt << "\n";
    }
    auto dofs = mesh->getAllDofs();
    auto edges = mesh->getAllEdges();
    ofstream dofs_file("./test/dofs.txt",ios::out);
    if(dofs_file.is_open()) {
      for(auto dof : dofs) {
        Point<2> pnt = dof.getPoint();
        dofs_file << pnt << "\n";
      }
      dofs_file.close();
    }
    ofstream edges_file("./test/edges.txt",ios::out);
    if(edges_file.is_open()) {
      for (auto enddofs: edges) {
        Point<2> start = enddofs.at(0).getPoint();
        Point<2> end = enddofs.at(1).getPoint();
        edges_file << start << " " << end << "\n";
      }
      edges_file.close();
    }

}

void test_refine() {
  cout << "test refine in EasyMesh:\n";
     EasyMesh *mesh = new EasyMesh("D",1);
     cout << "before refine:\n";
    int numgrid = mesh->getNumGrid();
    cout << "number of grids is:" << numgrid << endl;
    int numdofs = mesh->getTotalNumDofs();
    cout << "number of dofs is:" << numdofs << endl;
    int numedges = mesh->getNumEdge();
    cout << "number of edges is:" << numedges << endl;
     mesh->refine();
     cout << "after refine:\n";
    numgrid = mesh->getNumGrid();
    cout << "number of grids is:" << numgrid << endl;
    numdofs = mesh->getTotalNumDofs();
    cout << "number of dofs is:" << numdofs << endl;
    numedges = mesh->getNumEdge();
    cout << "number of edges is:" << numedges << endl;
    auto bnddofs = mesh->getBoundaryDofs();
    cout << "bnddofs size:" << bnddofs.size() <<"\n";
    // for (auto dof : bnddofs) {
    //   int index = dof.getGlobalIndex();
    //   int bndmark = mesh->getBoundaryMark(index);
    //   Point<2> pnt = dof.getPoint();
    //   // cout << "index: " << index <<", bndmark:" << bndmark << "\n";
    //   cout << pnt << "\n";
    // }
    auto dofs = mesh->getAllDofs();
    cout << "dofs size:" << dofs.size() <<"\n";
    vector<vector<Dofs<2>>> edges = mesh->getAllEdges();
    cout << "edges size:" << edges.size() <<"\n";
    ofstream dofs_file("./test/dofs_refine.txt",ios::out);
    if(dofs_file.is_open()) {
      for(auto dof : dofs) {
        Point<2> pnt = dof.getPoint();
        dofs_file << pnt << "\n";
      }
      dofs_file.close();
    }
    ofstream edges_file("./test/edges_refine.txt",ios::out);
    if(edges_file.is_open()) {
      for (auto enddofs: edges) {
        Point<2> start = enddofs.at(0).getPoint();
        Point<2> end = enddofs.at(1).getPoint();
        edges_file << start << " " << end << "\n";
      }
      edges_file.close();
    }


};
int main() {
	//test_easymesh();
	// test_distributeDofs(1);
	// test_distributeDofs(2);
	// test_distributeDofs(3);
	// test_distributeDofs(4);
  test_refine();
    return 0;
}
